package com.excel.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class Excel2003 {

	public static List<String> getSheetNames(InputStream fin)throws Exception{
		InputStream din = null;
		final List<String> sheetNames = new ArrayList<String>();
		try{
		    POIFSFileSystem poifs = new POIFSFileSystem(fin);  
		    din = poifs.createDocumentInputStream("Workbook");  
		    HSSFRequest req = new HSSFRequest();  
		    req.addListener(new HSSFListener() {
				public void processRecord(Record record) {
					if(record.getSid()==BoundSheetRecord.sid){
							BoundSheetRecord bsr = (BoundSheetRecord) record;
							sheetNames.add(bsr.getSheetname());
					}
				}
			}, BoundSheetRecord.sid);
	        HSSFEventFactory factory = new HSSFEventFactory();  
	        factory.processEvents(req, din);  
		}finally{
			if(din !=null){
				din.close();  
			}
		}
        return sheetNames;
	}
	
	public static void main(String[] args)throws Exception {
		String root = "/home/supertool/share/b.xls";
		List<String> names = getSheetNames(new FileInputStream(root));
		if(names!=null){
			for(String name :names){
				System.out.println(name);
			}
		}
	}
}
	
